var d={};
$.ajax({
		type : 'GET',
		async : true,
		url :'http://10.138.40.127:8080/GPSTrackWeb/gps/e6/gps?callbacks=abc&method=GetVehcileInfo&appkey=6bf3bb6d-cad1-43ca-ae51-43c7271ea30b&vehicle=-1&sessionid=&',
		jsonp:'callback',
		jsonpCallback:'abc',
		dataType : 'jsonp',
		error : function(e) {
			console.log(e);
		},
		success : function(e) {
			d = e;
			console.log(e)	
			$(function(){
				
				Map.init();
				
				$(".btn-serach").on("click", function(){
					alert('aaaa');
					console.log('!!!!!!!!!!!!!!!!!!!')	;
				});
				window.setInterval(function(){
					clusterLayer.removeAllFeatures();
					Map.setCluster();
				},40000); 
			})
		}
	});
var Map = {};
var map = null;
var layer = null, clusterLayer = null, selectFeature = null; 
Map.init = function(){
	map = new SuperMap.Map("map", { controls: [
			new SuperMap.Control.ScaleLine(),
			new SuperMap.Control.Zoom(),
			new SuperMap.Control.Navigation({
				dragPanOptions: {
					enableKinetic: true
				}
			})
		], 
		units: "m"
	});
	// layer = new SuperMap.Layer.CloudLayer();
	layer = new SuperMap.Layer.CloudLayer({url:"http://t2.supermapcloud.com/FileService/image"});
	map.addLayer(layer);
	map.setCenter(new SuperMap.LonLat(11584493.820190, 3589061.070760), 5);

	clusterLayer = new SuperMap.Layer.ClusterLayer("Cluster", {
        isDiffused: true,
        clusterStyles:[
            {
                "count": 20,//子节点小于等于10的聚散点
                "style":{
                    fontColor:"#ffffff",
                    graphic:true,
                    externalGraphic: "css/imgs/cluster-1.png",
                    graphicWidth: 60,
                    graphicHeight: 60,
                    labelXOffset: 0,
                    labelYOffset: 0
                }
            },
            {
                "count":"moreThanMax",//子节点大于10的聚散点
                "style":{
                    fontColor:"#ffffff",
                    graphic:true,
                    externalGraphic: "css/imgs/cluster-2.png",
                    graphicWidth: 66,
                    graphicHeight: 66,
                    labelXOffset: 0,
                    labelYOffset: 0
                }
            }
	   ]}
    );
	map.addLayers([clusterLayer]);

    //添加选择控件
    selectFeature = new SuperMap.Control.SelectFeature(
        [clusterLayer], 
        {
            //点击事件
            onSelect: function(e) {
				console.log(e)
                //如果点击的对象是单个点，就弹出信息窗
                if(e.isPoint) {
                    Map.popup = null;
                    var h =  '<div class="map-popup-sm">';
                        h += '  <div class="content">';
                        h += '      <div><label>车牌号:</label><span>'+ e.attributes.Vehicle +'</span></div>';
                        h += '      <div><label>经度:</label><span>'+ e.attributes.smx +'</span></div>';
                        h += '      <div><label>纬度:</label><span>'+ e.attributes.smy +'</span></div>';
						h += '      <div><label>速度:</label><span>'+ e.attributes.Speed +'</span></div>';
                        h += '  </div>';
                        h += '</div>';
                    Map.popup = new SuperMap.Popup.FramedCloud("popup-near",
                        new SuperMap.LonLat(e.geometry.x, e.geometry.y), 
                        new SuperMap.Size(200, 85),
                        h,
                        null,
                        true,
                        null
                    );
                    Map.popup.autoSize = false;
                    Map.popup.panMapIfOutOfView = true;
                    Map.popup.relativePosition = "tr";
                    Map.popup.closeOnMove = true;
                    map.removeAllPopup();
                    map.addPopup(Map.popup);
                }
                //如果点击的对象是聚合点, 就散开这些聚合点
                else if(e.isCluster) {
                    //获取聚散点的坐标集合
                    var xs = [], ys = [];
                    for(var i=e.children.length; i--;){
                        var geo = e.children[i].geometry;
                        xs.push(geo.x);
                        ys.push(geo.y);
                    }
                    //坐标X集合按照从小到大排序
                    xs.sort(function(a,b){
                        return a-b
                    });
                    //坐标Y集合按照从小到大排序
                    ys.sort(function(a,b){
                        return a-b
                    });
                    //根据坐标集合计算聚合点的边界
                    var bounds = new SuperMap.Bounds();
                    bounds.extend(new SuperMap.LonLat(xs[0], ys[0]));
                    bounds.extend(new SuperMap.LonLat(xs[xs.length - 1], ys[ys.length - 1]));
                    //散开边界内的点
                    clusterLayer.displayFeatures(bounds);
                }
            },
            multiple: false
        }
    );
    map.addControl(selectFeature);
    //激活选择控件
    selectFeature.activate();

	Map.setCluster();
}

Map.setCluster = function() {
	var fs1 = Map.getCoords();
	clusterLayer.addFeatures(fs1);
}


//添加点
Map.getCoords = function(){
	var t= window.setInterval(function(){
		if(typeof(d.result)=='undefined'){
			
		}else{
				console.log(d.result.data)
				clearInterval(t);
			}
	},100);
	var ps = [];
	var arr = d.result.data;
	console.log(arr)
	for(var i=0; i<arr.length; i++) {
		var smx = arr[i].Lon;
		var smy = arr[i].Lat;
		var x =arr[i].Lon;
		var y =arr[i].Lat;
		var Speed = arr[i].Speed;;
		var Vehicle =arr[i].Vehicle;
		var lonlat = Dituhui.latLonToMeters( new SuperMap.LonLat(x, y) );
		var f = new SuperMap.Feature.Vector();
		f.geometry = new SuperMap.Geometry.Point(lonlat.lon, lonlat.lat);
		f.style = {
			pointRadius: 4,
			graphic:true,
			externalGraphic:"css/imgs/online-0.png",
			graphicWidth: 30,
			graphicHeight: 30
		};
        f.attributes = {
			Vehicle:Vehicle,
            smx: smx,
			smy: smy,
			Speed:Speed,
        }
		ps.push(f);
	}
	return ps; 
}
